local DIY = require "packages/diy_utility/diy_utility"

local yinhan = fk.CreateSkill{
  name = "xiaobai__yinhan",
  tags = {Skill.Compulsory},
  dynamic_desc = function(self, player)
    if player:getMark("@xiaobai__yinhan-color") == "red" then
      return "xiaobai__yinhan_red"
    elseif player:getMark("@xiaobai__yinhan-color") == "black" then
      return "xiaobai__yinhan_black"
    end
    return "xiaobai__yinhan"
  end,
}

yinhan:addEffect(fk.BeforeCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if (not player:hasSkill(self))  or player:getMark("@xiaobai__yinhan-color") == 0 then return false end
    for _, move in ipairs(data) do
      if move.from == player and move.moveReason == fk.ReasonUse then
        local use = player.room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true).data
        if (not use.card:isVirtual() or #use.card.subcards == 0) and use.card.type ~= Card.TypeEquip and use.card.sub_type ~= Card.SubtypeDelayedTrick then
          for _,info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand then
              return true
            end
          end
        end
      elseif move.from == player and move.moveReason == fk.ReasonResponse and player:getMark("@xiaobai__yinhan-color") == "black" then
        return true
      elseif move.from == player and move.moveReason == fk.ReasonDiscard and player:getMark("@xiaobai__yinhan-color") == "red" then
        return true
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    if player:getMark("@xiaobai__yinhan-color") == "red" then
      for _, move in ipairs(data) do
        if move.moveReason == fk.ReasonUse then
          for _, info in ipairs(move.moveInfo) do
            DIY.hideCards(player,{info.cardId})
          end
          move.moveInfo = {}
        else
          for _, info in ipairs(move.moveInfo) do
            player.room:recastCard({info.cardId},player,yinhan.name)
          end
          move.moveInfo = {}
        end
      end
    elseif player:getMark("@xiaobai__yinhan-color") == "black" then
      for _, move in ipairs(data) do
        if move.moveReason == fk.ReasonUse then
          for _, info in ipairs(move.moveInfo) do
            DIY.showCards(player, {info.cardId})
          end
          move.moveInfo = {}
        else 
          local others = player.room:getOtherPlayers(player)
          local cards = {}
          for _, move in ipairs(data) do
            if move.from == player and move.moveReason == fk.ReasonResponse then
              for _, info in ipairs(move.moveInfo) do
                table.insertIfNeed(cards,info.cardId)
              end
              move.moveInfo = {}
            end
          end
          if #cards == 0 then return end
          player.room:askToYiji(player, {
            cards = cards,
            targets = others,
            min_num = #cards,
            max_num = #cards,
            skill_name = yinhan.name,
            prompt = "jimmo-give"
          })
        end
      end
    end
  end,
})
yinhan:addEffect(fk.AfterCardsMove, {
  can_refresh = function (self, event, target, player, data)
    if not player:hasSkill(self) then return end
    for _, move in ipairs(data) do
      if #move.moveInfo == 1 and move.toArea == Card.DiscardPile then
        return true
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    for _, move in ipairs(data) do
      if #move.moveInfo == 1 and move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          player.room:setPlayerMark(player,"@xiaobai__yinhan-color",Fk:getCardById(info.cardId):getColorString())
        end
      end
    end
  end
})
yinhan:addAcquireEffect(function (self, player)
  player.room.logic:getEventsByRule(GameEvent.MoveCards,1,function (e)
    for _, move in ipairs(e.data) do
      if #move.moveInfo == 1 and move.toArea == Card.DiscardPile then
        for _, info in ipairs(move.moveInfo) do
          player.room:setPlayerMark(player,"@xiaobai__yinhan-color",Fk:getCardById(info.cardId):getColorString())
          return true
        end
      end
    end
  end,Player.HistoryGame)
end)

yinhan:addLoseEffect(function (self, player)
  player.room:setPlayerMark(player,"@xiaobai__yinhan-color",0);
end)

yinhan:addEffect("prohibit",{
  prohibit_use = function(self, player, card)
    if not player:hasSkill(yinhan.name) then return end
    if player:getMark("@xiaobai__yinhan-color") == "black" then
      if card:getMark("@@ShownCards-inhand") > 0 then
        return true
      end
    elseif player:getMark("@xiaobai__yinhan-color") == "red" then
      if card:getMark("@@ShownCards-inhand") == 0 then
        return true
      end
    end
  end
})

Fk:loadTranslationTable{
  ["@xiaobai__yinhan-color"] = "引瀚",
  ["xiaobai__yinhan"] = "引瀚",
  [":xiaobai__yinhan"] = "锁定技，若最后进入弃牌堆的是黑色牌，你以明置替代使用，以交给替代打出；反之，你以暗置替代使用，以重铸替代弃置。<a href=':xiaobai__yinhan1'>详细</a>",
  [":xiaobai__yinhan1"] = "若上一张单独进入弃牌堆的是黑色牌，你的非转化即时牌以明置替代使用，以交给其他角色替代打出手牌；"..
                          "若上一张单独进入弃牌堆的是红色牌，你的非转化即时牌以将明置牌改为暗置替代使用，以重铸手牌替代弃置手牌。<a href=':xiaobai__yinhan-detail'>怎么这么复杂？</a>",
  
  [":xiaobai__yinhan_black"] = "锁定技，<font color=\"#E0DB2F\">若最后进入弃牌堆的是黑色牌，你以明置替代使用，以交给替代打出；</font>"..
    "反之，你以暗置替代使用，以重铸替代弃置。",
  [":xiaobai__yinhan_red"] = "锁定技，若最后进入弃牌堆的是黑色牌，你以明置替代使用，以交给替代打出；<font color=\"#E0DB2F\">反之，你以暗置替代使用，以重铸替代弃置。</font>",

  ["xiaobai__yinhan-detail"] = "引瀚",
  [":xiaobai__yinhan-detail"] = "看不懂此技能请私信问候EDG。\n<a href=':xiaobai__yinhan-real-detail'>如何问候？</a>",
  ["xiaobai__yinhan-real-detail"] = "引瀚",
  [":xiaobai__yinhan-real-detail"] = "易大剧：电话：+49（德）015259154728        QQ：814516560\n<a href=':xiaobai__yinhan-real-real-detail'>我真的只是想看看技能描述</a>",
  ["xiaobai__yinhan-real-real-detail"] = "引瀚",
  [":xiaobai__yinhan-real-real-detail"] = "①若上一张单独进入弃牌堆的是黑色牌：你无法使用明置牌；你的非转化即时牌暗置牌因使用而离开手牌区时，你取消之改为明置此牌；"..
                            "牌因打出离开你的手牌区时，你将此牌交给一名其他角色；"..
                          "②若上一张单独进入弃牌堆的是红色牌：你无法使用暗置牌；你的非转化即时明置牌因使用而离开手牌区时，你取消之改为暗置此牌；"..
                            "当你的牌因弃置而进入弃牌堆时，你取消之改为重铸此牌。",
  ["$xiaobai__yinhan1"] = "落笔不经意，动乃成云鹤。",
  ["$xiaobai__yinhan2"] = "作字同行兵，斧钺入体势。"
}

return yinhan
